/**  
* @Title: ShopTablesService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月2日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gexin.rp.sdk.base.IPushResult;
import com.github.pagehelper.PageHelper;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.mapper.cityRun.CrShopTablesMapper;
import com.lemon.mapper.cityRun.CrShopTablesServerMapper;
import com.lemon.mapper.cityRun.CrShopUserMapper;
import com.lemon.model.cityRun.CrShopTables;
import com.lemon.model.cityRun.CrShopTablesServer;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.utils.appPush.AppPush;
import com.lemon.utils.wx.WeiXinUtils;

/**
 * <p>Title: ShopTablesService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月2日
 * @版本 V 1.0 
 */
@Service
public class ShopTablesServiceImpl implements ShopTablesService {

	private static final Logger log = LoggerFactory.getLogger(ShopTablesServiceImpl.class);

	@Resource
	private CrShopTablesServerMapper tablesServerMapper;
	@Resource
	private CrShopTablesMapper tabMapper;

	@Resource
	private CrShopUserMapper uMapper;
	/**
	 * <p>
	 * Title: selectServerListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param server
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@Override
	public List<CrShopTablesServer> selectServerListBySelective(CrShopTablesServer server) throws Exception {
		PageHelper.startPage(server.getPageNum() , server.getPageSize());
		return tablesServerMapper.selectListBySelective(server);
	}

	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tab
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月2日
	 */
	@Override
	public List<CrShopTables> selectListBySelective(CrShopTables tab) throws Exception {
		PageHelper.startPage(tab.getPageNum() , tab.getPageSize());
		return tabMapper.selectListBySelective(tab);
	}

	/**
	 * <p>
	 * Title: updateBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tab
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@Override
	public int updateBySelective(CrShopTables tab) throws Exception {
		List<CrShopTables> list = tabMapper.selectListBySelective(tab);
		CrShopTables tb = new CrShopTables();
		if (list.size() > 0) {
			tb = list.get(0);
			String wxacode = WeiXinUtils.getWxacode("pages/shopInfo/shopInfo" ,
					tb.getId() + "-" + tb.getShopId());
			tab.setQrcodePic(wxacode);
		}
		return tabMapper.updateByPrimaryKeySelective(tab);
	}

	/**
	 * <p>
	 * Title: insertBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tab
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月2日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int insertBySelective(CrShopTables tab) throws Exception {
		int selective = tabMapper.insertSelective(tab);
		//返回主键之后封装到二维码中 然后修改二维码图片地址
		if (selective > 0) {

			//生成微信小程序的菊花码
			String wx = IdTypeHandler.encode(Long.parseLong(tab.getId())) + "-" + tab.getShopId();
			String wxacode = WeiXinUtils.getWxacode("pages/shopInfo/shopInfo" , wx);
			/*
			 * //创建二维码
			 * HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
			 * .getRequest();
			 * String realPath = request.getSession().getServletContext().getRealPath("/");
			 * //将图片缓存到下面的文件下面
			 * File destFile = new File(realPath + StaticValues.UPLOAD_TEMP_PATH);
			 * //不存在则创建
			 * if (!destFile.exists()) {
			 * destFile.mkdirs();
			 * }
			 * //生成新的文件名称 //不含后缀
			 * String newFileName = StringUtil.getUUID();
			 * //生成二维码
			 * JSONObject js = new JSONObject();
			 * tab.setId(IdTypeHandler.encode(Long.parseLong(tab.getId())));
			 * js.put("id" , tab.getId());
			 * js.put("title" , tab.getTitle());
			 * newFileName = QrCodeUtils2.encode(js.toJSONString() , null , destFile.getAbsoluteFile() + "/" ,
			 * newFileName , true);
			 * //二维码的绝对路径
			 * String filepath = destFile.getAbsoluteFile() + "/" + newFileName;
			 * //上传到七牛存储空间
			 * String upload = QNFileUpload.upload(filepath , newFileName);
			 * //读取本地缓存二维码图片文件 , 并删除缓存文件
			 * File f = new File(filepath);
			 * f.delete();
			 */
			tab.setQrcodePic(wxacode);
			//将桌台二维码保存到数据库
			tab.setId(IdTypeHandler.encode(Long.parseLong(tab.getId())));
			tabMapper.updateByPrimaryKeySelective(tab);

		}
		return selective;
	}

	/**
	 * <p>
	 * Title: insertTableServer
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param server
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年12月13日
	 */
	@Override
	public int insertTableServer(CrShopTablesServer server) throws Exception {
		int i = tablesServerMapper.insertSelective(server);

		CrShopUser u = new CrShopUser();
		String title = server.getTitle();
		server.setTitle(title + "需要服务");

		u.setWhere(" and b.id = ? " , IdTypeHandler.decode(server.getShopId()));
		CrShopUser user = uMapper.selectBySelective(u);

		try {
			CrShopTables tb = new CrShopTables();
			tb.setTitle(title);
			tb.setShopId(server.getShopId());
			tb.setOrderBy(" order by modify_time desc");
			List<CrShopTables> tables = tabMapper.selectListBySelective(tb);
			AppPush push = new AppPush();
			CrShopTables table = tables.size() > 0 ? tables.get(0) : null;
			log.info("获取当前桌台信息:{}" , table);
			if (table == null) {
				return 0;
			}
			log.info("开始推送桌台呼叫服务");
			String ss = table.getSort() > 20 ? table.getTitle() : table.getSort() + "";
			push.setCid(user.getCid());
			push.setContent( ss );
			push.setTitle("桌台服务");
			push.setType(2);
			push.setText("您有一个桌台服务,点击查看");
			IPushResult pushMsgToSingle = AppPush.pushMsgToSingle(push);
			log.info(pushMsgToSingle.getResponse().toString());
		} catch (Exception e) {
		}
		return i;
	}
}
